//用于存放登录用户
let arr = [];
//引入
let express = require("express");
let mysql = require("mysql");
let form = require("formidable");
let cookie = require("cookie-parser");
let urll = require("url");
//配置数据库
let connection = mysql.createConnection({
      host: "127.0.0.1",
      user: "root",
      password: "root",
      database: "h5-67",
      multipleStatements: true//设置可以同时查询多条语句
})
//连接数据库
connection.connect();
//创建应用
let app = express();
//创建服务
let server = require("http").Server(app);
//引入socket.io
let io = require("socket.io")(server);
//使用cookie
app.use(cookie());
//设置静态资源路径
app.use(express.static("node_modules"))
//设置模板引擎
app.set("view engine", "ejs");
//设置视图路径
app.set("views", "./views");
//加载首页
app.get("/", (req, res) => {
      res.render("index");
      res.end();
})
//登录路由
app.get("/login", (req, res) => {
      res.render("login");
      res.end();
})
//执行登录
app.post("/dologin", (req, res) => {
      let form2 = new form.IncomingForm();
      form2.parse(req, (err, fields) => {
            let { username, password } = fields;
            var sql = "select * from users where username=?"
            let uname = [username];
            var sql = mysql.format(sql, uname);
            connection.query(sql, (error, results) => {
                  if (error) { return; }
                  if (!error) {
                        if (results.length > 0) {
                              if (results[0].password == password) {
                                    res.cookie("usernamex", username)

                                    //将所有登录的用户名存放在arr中
                                    arr.push(results[0]);
                                    console.log(arr);

                                    console.log("登录成功");
                                    console.log(`尊敬的${results[0].username}用户,您好`);
                                    res.redirect('/chat?id=' + results[0].id)
                                    res.end();
                              } else {
                                    console.log("密码错误");
                                    res.redirect("/");
                                    res.end();
                              }
                        } else {
                              console.log("用户名错误");
                              res.redirect("/");
                              res.end();
                        }
                  }
            })
      })
})
//设置聊天室路由
app.get('/chat', (req, res) => {
      let url = req.url;
      let id = urll.parse(url, true).query.id;
      let sql2 = 'select * from users';
      //获取当前cookie值(无最大时间限制)
      // let user = req.cookies.usernamex;
      // console.log(user);




      connection.query(sql2, (error, results) => {
            // console.log(results);
            res.render('chat', { data: results, id: id });
            res.end();
      })
})

//退出登录
app.get('/outlogin', (req, res) => {
      res.clearCookie('usernamex');
      res.end();
})


//连接
io.on("connection", socket => {
      socket.on("h571", res => {
            // console.log(res);
            socket.broadcast.emit("h571", res);
      })
})

//设置监听端口
server.listen(8090);